<?php
/**
 * A rail_container might be use to organize other rail objects.
 *
 * PHP versions 5
 *
 * Copyright (C)
 * This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
 * of ERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software
 * Fundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
 *
 * @author Bastian Schröder <bs@chemanet.de>
 * @version 2008-08-25
 * @package rail
 */
class rail_container extends rail_object {

	/**
	 * Attributes which are not writeable from outside. The array contains only the name
	 * of protected attributes not the attribute itself.
	 *
	 * @var array
	 */
	protected $protected_attributes = array( OBJ_TIME_CREATE, OBJ_TIME_LASTWRITE, CONTAINER_SEQUENCE );


	/**
	 * Return a copy of this sequence.
	 *
	 * @exception rail_readpermission_exception
	 * @param rail_object $pEnvironment The new environment
	 * @param bool $pRecursive (optional)
	 * @return rail_object
	 */
	public function copy ( $pEnvironment, $pRecursive = false ) {
		// get copy from this object, but do not copy inventory
		$copy = parent::copy( $pEnvironment, false );

		// copy sequences inventory if pRecursive is set to true
		if( $pRecursive ){
			$sequence = array();
			foreach( $this->get_sequence() as $object ){
				if( $object->get_id() == $copy->get_id() || !$object->is_readable() )
					continue;

				$sequence[] = $object->copy( $copy, $pRecursive );
			}
			$copy->set_sequence( $sequence );
		}
		// remove CONTAINER_SEQUENCE from sequence if pRecursive is set to false and CONTAINER_SEQUENCE exists
		else if( $copy->get_attribute( 'CONTAINER_SEQUENCE' ) )
			$this->rail_connector->send_request( "DELETE FROM objects_attributes WHERE `object`='" . $copy->get_id() . "' AND `attribute`='CONTAINER_SEQUENCE'" );

		return $copy;
	}


	/**
	 * Get container sequence. Returned array of rail_object.
	 *
	 * @return array
	 */
	public function get_sequence ( $pRailTypes = "", $pRecursive = false  ) {
		$array = array();
		$inventory = $this->get_inventory( $pRailTypes );
		$tmp_inventory = $inventory;

		// get CONTAINER_SEQUENCE
		foreach( explode( '-', $this->get_attribute( CONTAINER_SEQUENCE ) ) as $id ){
			if( strlen( $id ) <= 0 ) continue;

			for($i=0; $i < count($inventory); $i++ ){
				if( $id == $inventory[ $i ]->get_id() ){
					$array[] = $inventory[ $i ];
					unset( $tmp_inventory[ $i ] );
					break;
				}
			}
		}

		return array_merge( $array, $tmp_inventory );
	}


	/**
	 * Set sequence for this container. Require a array of rail_object objects.
	 *
	 * @exception rail_writepermission_exception
	 * @param array $pRailObjects
	 */
	public function set_sequence ( $pRailObjects ) {
		// check write permission
		 if( !$this->is_writeable() )
		 	throw new rail_writepermission_exception( $this );

		$ids = array();
		foreach( $pRailObjects as $object )
			if( $object->get_environment()->get_id() == $this->get_id() && !in_array( $object->get_id(), $ids ) )
				$ids[] = $object->get_id();

		$value = implode( '-', $ids );
		if( $this->get_attribute( CONTAINER_SEQUENCE ) === false )
			$sql = "INSERT INTO objects_attributes (`object`,`attribute`,`value`) VALUES ('" . $this->get_id() . "','CONTAINER_SEQUENCE','$value')";
		else
			$sql = "UPDATE objects_attributes SET `value`='$value' WHERE `object`='" . $this->get_id() . "' AND `attribute`='CONTAINER_SEQUENCE'";

		$this->rail_connector->send_request( $sql );

		// update attribute cache
		$this->cache[ 'attributes' ][ 'CONTAINER_SEQUENCE'  ] = $value;

		// update OBJ_TIME_LASTWRITE attribute
		$this->rail_connector->send_request( "UPDATE objects_attributes SET `value`='" . time() . "' WHERE `object`='" . $this->get_id() . "' AND `attribute`='OBJ_TIME_LASTWRITE'" );
	}

} // End of rail_container
?>